package com.yserp.kpre.service.impl;
import com.yserp.kpre.service.TKpMonthHServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.yserp.kpre.entity.TKpMonthHEntity;
import com.yserp.kpre.entity.TKpMonthIEntity;

import org.jeecgframework.core.util.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;


@Service("tKpMonthHService")
@Transactional
public class TKpMonthHServiceImpl extends CommonServiceImpl implements TKpMonthHServiceI {
	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((TKpMonthHEntity)entity);
 	}
	
	public void addMain(TKpMonthHEntity tKpMonthH,
	        List<TKpMonthIEntity> tKpMonthIList){
			//保存主信息
			this.save(tKpMonthH);
		
			/**保存-月报项目*/
			for(TKpMonthIEntity tKpMonthI:tKpMonthIList){
				//外键设置
				try{
					tKpMonthI.setMhYuefenb(tKpMonthH.getMhYuefenb());
					tKpMonthI.setMhYuefene(tKpMonthH.getMhYuefene());
				}catch (Exception e){

				}

				tKpMonthI.setMhHdid(tKpMonthH.getId().toString());
				this.save(tKpMonthI);
			}
			//执行新增操作配置的sql增强
 			this.doAddSql(tKpMonthH);
	}

	
	public void updateMain(TKpMonthHEntity tKpMonthH,
	        List<TKpMonthIEntity> tKpMonthIList) {
		//保存主表信息
		if(StringUtil.isNotEmpty(tKpMonthH.getId())){
			try {
				TKpMonthHEntity temp = findUniqueByProperty(TKpMonthHEntity.class, "id", tKpMonthH.getId());
				MyBeanUtils.copyBeanNotNull2Bean(tKpMonthH, temp);
				this.saveOrUpdate(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			this.saveOrUpdate(tKpMonthH);
		}
		//===================================================================================
		//获取参数
		Object id0 = tKpMonthH.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-月报项目
	    String hql0 = "from TKpMonthIEntity where 1 = 1 AND mH_HDID = ? ";
	    List<TKpMonthIEntity> tKpMonthIOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-月报项目
		if(tKpMonthIList!=null&&tKpMonthIList.size()>0){
		for(TKpMonthIEntity oldE:tKpMonthIOldList){
			boolean isUpdate = false;
				for(TKpMonthIEntity sendE:tKpMonthIList){
					//需要更新的明细数据-月报项目
					if(oldE.getId().equals(sendE.getId())){
		    			try {

							try{
								sendE.setMhYuefenb(tKpMonthH.getMhYuefenb());
								sendE.setMhYuefene(tKpMonthH.getMhYuefene());
							}catch (Exception e){

							}


							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-月报项目
		    		super.delete(oldE);
	    		}
	    		
			}
			//3.持久化新增的数据-月报项目
			for(TKpMonthIEntity tKpMonthI:tKpMonthIList){
				if(oConvertUtils.isEmpty(tKpMonthI.getId())){
					//外键设置
					tKpMonthI.setMhHdid(tKpMonthH.getId().toString());
					try{
						tKpMonthI.setMhYuefenb(tKpMonthH.getMhYuefenb());
						tKpMonthI.setMhYuefene(tKpMonthH.getMhYuefene());
					}catch (Exception e){

					}
					this.save(tKpMonthI);
				}
			}
		}
		//执行更新操作配置的sql增强
 		this.doUpdateSql(tKpMonthH);
	}

	
	public void delMain(TKpMonthHEntity tKpMonthH) {
		//删除主表信息
		this.delete(tKpMonthH);
		//===================================================================================
		//获取参数
		Object id0 = tKpMonthH.getId();
		//===================================================================================
		//删除-月报项目
	    String hql0 = "from TKpMonthIEntity where 1 = 1 AND mH_HDID = ? ";
	    List<TKpMonthIEntity> tKpMonthIOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(tKpMonthIOldList);
	}
	
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(TKpMonthHEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(TKpMonthHEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(TKpMonthHEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,TKpMonthHEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{sys_org_code}",String.valueOf(t.getSysOrgCode()));
 		sql  = sql.replace("#{sys_company_code}",String.valueOf(t.getSysCompanyCode()));
 		sql  = sql.replace("#{bpm_status}",String.valueOf(t.getBpmStatus()));
 		sql  = sql.replace("#{mh_yuefenb}",String.valueOf(t.getMhYuefenb()));
 		sql  = sql.replace("#{mh_yuefene}",String.valueOf(t.getMhYuefene()));
 		sql  = sql.replace("#{mh_remark}",String.valueOf(t.getMhRemark()));
 		sql  = sql.replace("#{mh_status}",String.valueOf(t.getMhStatus()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
}